﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_v_cartorder : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //CommonBLL.RegisterCKFinder(CKEditorBody);
            //PanelEdit.Visible = false;
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {
                LoadDataInfo(Request.QueryString["itemid"].ToString().Trim());               
            }
        }
    }
    private void LoadDataInfo(string ItemID)
    {
        DataTable dt = new BLL.CmsBLL().CartOrder_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {
            //hdItemID.Value = dt.Rows[0]["OrderId"].ToString();
            string OrderId = dt.Rows[0]["OrderId"].ToString();
            ltSender.Text = dt.Rows[0]["Fullname"].ToString();
            ltEmail.Text = dt.Rows[0]["Email"].ToString();
            ltPhone.Text = dt.Rows[0]["PhoneNumber"].ToString();
            lt_Address.Text = dt.Rows[0]["Address"].ToString();
            ltCreated.Text = DateTime.Parse(dt.Rows[0]["Created"].ToString()).ToString("dd/MM/yyyy");
            ltOrderID.Text = OrderId;
           
            DataTable dtOrderDetail = new BLL.CmsBLL().GetOrderDetail(OrderId);
            if (dtOrderDetail != null && dtOrderDetail.Rows.Count > 0)
            {
                string statusId = dtOrderDetail.Rows[0]["StatusName"].ToString();
                RendeOrderDetail(dtOrderDetail);
                lt_Status.Text = dt.Rows[0]["StatusName"].ToString();
            }
        }
        else
            lblMessageView.Text = "Load data failt!";
    }

    private void RendeOrderDetail(DataTable dtOrderDetail)
    {
        StringBuilder Html = new StringBuilder();
        Html.Append(
            "		<table id=\"dtcart\" class=\"carttable\">\n" +
            "			<tr>\n" +
            "				<th class=\"stt\">STT</th>\n" +
            "				<th class=\"name\">Sản phẩm</th>\n" +
            "				<th class=\"price\">Giá (vnđ)</th>\n" +
            "				<th class=\"sl\">Số lượng</th>\n" +
            "				<th class=\"total\">Thành tiền</th>\n" +
            "				<th class=\"function\">&nbsp;</th>\n" +
            "			</tr>\n"
            );
        double total = 0;
        string productUrl = string.Empty;
        int itemnumber = 0;
        double price = 0;
        string productCode = string.Empty;
        for (int i = 0; i < dtOrderDetail.Rows.Count; i++)
        {
            productCode = dtOrderDetail.Rows[i]["ProductID"].ToString();
            itemnumber = Convert.ToInt16(dtOrderDetail.Rows[i]["Quantity"]);
            price = Convert.ToDouble(dtOrderDetail.Rows[i]["Price"]);
            string ProductName = dtOrderDetail.Rows[i]["ProductName"].ToString();
            Html.Append(

            "			<tr class=\"odd\">\n" +
            "				<td class=\"stt\">" + (i + 1) + "</td>\n" +
            "				<td class=\"name\"><a href=\"" + productUrl + "\">" + ProductName + "</a></td>\n" +
            "				<td class=\"price\">" + price.ToString("#,###") + "</td>\n" +
            "				<td class=\"sl\">" + itemnumber + "</td>\n" +
            "				<td class=\"total\" >" + (price * itemnumber).ToString("#,###") + "</td>\n" +
            "				</td>\n" +
            "			</tr>\n");
            total += (price * itemnumber);
        }
        Html.Append(
            "			<tr>\n" +
            "				<td class=\"totalsum\" colspan=\"6\">\n" +
            "					\n" +
            "					Tổng tiền: " + total.ToString("#,###") + " vnđ <br />\n" +
            "					\n" +
            "				</td>\n" +
            "			</tr>\n" +
            "		</table>\n");
        ltr_Orderdetail.Text = Html.ToString();
    }

    protected void btnEditItem_Click(object sender, EventArgs e)
    {
        PanelView.Visible = false;
        //PanelEdit.Visible = true;    
        //LoadDataInfoEdit(hdItemID.Value);     
    }
    //private void LoadDataInfoEdit(string ItemID)
    //{
    //    DataTable dt = new BLL.CmsBLL().CartOrder_ViewItemByID(ItemID);
    //    if (dt.Rows.Count > 0)
    //    {
    //        txtSender.Text = dt.Rows[0]["Sender"].ToString();
    //        txtEmail.Text = dt.Rows[0]["Email"].ToString();
    //        txtPhone.Text = dt.Rows[0]["Phone"].ToString();
    //        txtTitle.Text = dt.Rows[0]["Title"].ToString();
    //        CKEditorBody.Text = dt.Rows[0]["Body"].ToString();             
    //    }
    //    else
    //        lblMessageEdit.Text = "Load data failt!";
    //}      
    //protected void lbtUpdateItem_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        new CmsBLL().CartOrder_UpDateItem(int.Parse(hdItemID.Value), txtTitle.Text, CKEditorBody.Text + "", txtSender.Text + "", txtEmail.Text + "", txtPhone.Text + "");
    //        lblMessageEdit.Text = "Cập nhật thành công!";           
    //    }
    //    catch (Exception ex)
    //    {
    //        lblMessageEdit.Text = "ERROR! " + ex.Message.ToString();
    //    }
    //}
}